package com.sinosoft.system.primary.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sinosoft.system.primary.dao.SysCredentialsDao;
import com.sinosoft.system.primary.entity.SysCredentials;


   /**
    * sys_credentials DaoJdbc实现类
    * Thu Dec 29 11:24:12 CST 2016 shangxp
    */ 
@Repository
public class SysCredentialsDaoJdbc implements SysCredentialsDao {
	private static Logger log = Logger.getLogger(SysCredentialsDaoJdbc.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int insertSysCredentials(SysCredentials sysCredentials) {
		String sqlStr = "insert into sys_credentials ( id, credentials, user_name, is_del) values(  ?, ?, ?, 0 )";
		log.debug(sqlStr+"\n"+sysCredentials.toString());
		try{
			Object[] args = {
				sysCredentials.getId(),
				sysCredentials.getCredentials(),
				sysCredentials.getUserName()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据添加异常 SQL："+sqlStr+"\n"+sysCredentials.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int updateSysCredentials(SysCredentials sysCredentials) {
		String sqlStr = "update sys_credentials set id = ? , credentials = ? , user_name = ?  where id = ?";
		log.debug(sqlStr+"\n"+sysCredentials.toString());
		try{
			Object[] args = {
					sysCredentials.getId(),
					sysCredentials.getCredentials(),
					sysCredentials.getUserName(),
					sysCredentials.getId()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据更新异常 SQL："+sqlStr+"\n"+sysCredentials.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int deleteSysCredentials(int id) {
		String sqlStr = "update sys_credentials set is_del = 1  where id = ?";
		try{
			Object[] args = {
					id
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return 0;
	}

	@Override
	public SysCredentials selectSysCredentials(int id) {
		String sqlStr = "select * from sys_credentials where is_del =0 and id = ?";
		log.debug(sqlStr+"\nID="+id);
		try{
			Object[] args = {
				id
			};
			return jdbcTemplate.queryForObject(sqlStr, args, new SysCredentialsMapper());
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return null;
	}

	@Override
	public SysCredentials selectSysCredentials(String[] queryKeys,Object[] queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from sys_credentials where is_del =0 " );
		if(queryKeys!=null && queryKeys.length!=0){
			for(int i=0;i<queryKeys.length;i++){
				sqlStr.append(" and "+queryKeys[i]+"=? ");
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), queryValues, new SysCredentialsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountSysCredentials() {
		String sqlStr = "select count(1) from sys_credentials where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.queryForObject(sqlStr, Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<SysCredentials> selectLimitSysCredentials(int start, int end) {
		String sqlStr = "select * from sys_credentials where is_del =0  limit ?,?";
		log.debug(sqlStr);
		try{
		Object[] args = {
				start,
				end
		};
			return jdbcTemplate.query(sqlStr, args, new SysCredentialsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<SysCredentials> selectAllSysCredentials() {
		String sqlStr = "select * from sys_credentials where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.query(sqlStr, new SysCredentialsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountSysCredentials(String[] queryKeys,Object[] queryValues) {
		StringBuffer sqlSb = new StringBuffer("select count(1) from sys_credentials where is_del = 0 ");
		if(queryKeys!=null && queryKeys.length!=0){
			for(int i=0;i<queryKeys.length;i++){
				sqlSb.append(" and "+queryKeys[i]+"=? ");
			}
		}		
		log.debug(sqlSb.toString());
		try{
			return jdbcTemplate.queryForObject(sqlSb.toString(), queryValues,Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlSb.toString()+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<SysCredentials> selectLimitSysCredentials(String[] queryKeys,Object[] queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from sys_credentials where is_del =0 " );
		if(queryKeys!=null && queryKeys.length!=0){
			for(int i=0;i<queryKeys.length-2;i++){
				sqlStr.append(" and "+queryKeys[i]+"=? ");
			}
		}
		sqlStr.append(" limit ?,?");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), queryValues, new SysCredentialsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<SysCredentials> selectAllSysCredentials(String[] queryKeys,Object[] queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from sys_credentials where is_del =0 " );
		if(queryKeys!=null && queryKeys.length!=0){
			for(int i=0;i<queryKeys.length;i++){
				sqlStr.append(" and "+queryKeys[i]+"=? ");
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), queryValues, new SysCredentialsMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	protected class SysCredentialsMapper implements RowMapper<SysCredentials> {
		@Override
		public SysCredentials mapRow(ResultSet arg0, int arg1) throws SQLException {
			SysCredentials sysCredentials = new SysCredentials();
			if(arg0!=null){
				sysCredentials.setId(arg0.getInt("id"));
				sysCredentials.setCredentials(arg0.getString("credentials"));
				sysCredentials.setUserName(arg0.getString("user_name"));
				}
			return sysCredentials;
		}  
	}
}

